//
//  HomeViewController.m
//  新浪微博OC
//
//  Created by  justinchou on 16/2/23.
//  Copyright © 2016年  justinchou. All rights reserved.
//

#import "HomeViewController.h"

NSString *HomeViewControllerCellID = @"HomeViewControllerCellID";

@interface HomeViewController ()
@property (nonatomic, strong) NSMutableArray *dataList;
@end

@implementation HomeViewController

#pragma  mark - 懒加载
-(NSMutableArray *)dataList{
    if (_dataList == nil) {
        _dataList = [[NSMutableArray alloc] init];
    }
    return _dataList;
}

#pragma  mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:HomeViewControllerCellID];
    
    [self setupRefreshControl];
}

- (void)loadData{
    
    int defaultIndex = 100;
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        int dataCount = (int)self.dataList.count;
        int startIndex = 0;
        int pullupIndex = 0;
        BOOL isPullUp = self.pullupView.isAnimating;
        
        if (dataCount == 0) {
            startIndex = defaultIndex;
        } else {
            startIndex = (int)[self.dataList.firstObject integerValue];
        }
        
        if (dataCount == 0) {
            pullupIndex = 0;
        } else {
            pullupIndex = (int)[self.dataList.lastObject integerValue];
        }
        
        //使用上拉控件是否在动画来表示将数据将在头还是尾
        if (isPullUp == false) {
            for (int i = 0; i < 10; i++) {
                [self.dataList insertObject:@(startIndex + 1 + i) atIndex:0];
            }
        } else {
            for (int i = 0; i < 10; i++) {
                [self.dataList addObject:@(pullupIndex - 1 - i)];
            }
        }
        
        //测试时推荐用此方法 若用线程sleep会卡死线程，无法进行其它操作
        dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)((double)NSEC_PER_SEC * 2.0));
        dispatch_after(when, dispatch_get_main_queue(), ^{
            [self.refreshControl endRefreshing];
            
            [self.pullupView stopAnimating];
            
            [self.tableView reloadData];
        });
        
    });
    
}

#pragma  mark - 数据源以及代理方法的实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:HomeViewControllerCellID forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", self.dataList[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //获取当前tableview的最大组号和这个组号内对应的最大组的cell的数量， 都减1
    int section = (int)self.tableView.numberOfSections - 1;
    int row = (int)[self.tableView numberOfRowsInSection:section] - 1;
    
    if (section == indexPath.section && row == indexPath.row && self.pullupView.isAnimating == false) {
        [self.pullupView startAnimating];
        [self loadData];
    }
    
}

@end

